import { Key } from 'react';
import { request } from '@inbiz/utils';
const version = InbizConfig.API_VERSION;

// 获取流程设计信息
export async function getProcessData(id?: string) {
    return request(`/edoc2Flow-web/process/design/getProcessData?id=${id || ''}&orgToken=&loginName=`);
}

// 获取设置变量列表
export const getVariateList = (params: {
    processId: any;
    pageIndex?: number;
    pageSize?: number;
}) => {
    return request('/edoc2Flow-web/process/manageMent/getProcessVar', {
        params: {
            processId: params.processId,
            pageIndex: params.pageIndex,
            pageSize: params.pageSize,
        },
    });
};

// 获取流程绑定页面的控件
export const getPageDetails = (params: { ismove?: boolean; processKey: string }) => {
    return request('/edoc2Flow-web/process/manageMent/getPageDetails', {
        params,
    });
};

// 获取版本管理列表
export const getVersionData = (params: {
    processKey: string;
    pageIndex: number;
    pageSize: number;
}) => {
    return request('/edoc2Flow-web/process/design/getProcessVers', {
        params: {
            key: params.processKey,
            pageIndex: params.pageIndex,
            pageSize: params.pageSize,
        },
    });
};

// 发布流程
export const processRelease = (id: string, type: string) => {
    return request('/edoc2Flow-web/process/manageMent/processRelease', {
        method: 'POST',
        data: type == 'id' ? { id } : { key: id },
    });
};

// 保存设置变量
export const saveVariate = (params: any) => {
    return request('/edoc2Flow-web/process/manageMent/saveOrUpdateProcessVar', {
        method: 'POST',
        requestType: 'form',
        data: params,
    });
};

// 子流程列表
export const getSubProcessList = (params: any) => {
    return request('/edoc2Flow-web/process/manageMent/getProcessList', {
        params: {
            searchContext: params.searchContext,
            pageIndex: params.pageIndex,
            pageSize: params.pageSize,
        },
    });
};

// 获取消息分组
export const getMessageGroup = () => {
    return request(`/inbiz/api/services/front/msgengine/v${version}/msgGroup/AppGroupList`, {
        params: {
            AppId: '',
            PageIndex: 1,
            PageSize: 999,
        },
    });
};

// 获取分组内消息列表
export const getMessageByGroupKey = (params: { GroupKey: Key; appId: string, PageIndex: number; PageSize: number; TemplateName?: string }) => {
    return request(`/inbiz/api/services/front/msgengine/v${version}/msgTemplate/templateByGroupKey`, {
        params: {
            GroupKey: params?.GroupKey,
            PageIndex: params?.PageIndex,
            PageSize: params?.PageSize,
            TemplateName: params?.TemplateName || '',
        },
        headers: {
            appId: params?.appId
        },
    });
};

// 新建流程和保存流程设计
export const saveOrUpdateProcessDesign = (params: any) => {
    return request('/edoc2Flow-web/process/design/saveOrUpdateProcessDesign', {
        method: 'POST',
        data: params,
    });
};

// 获取流程分组
export const getProcessGroup = () => {
    return request(`/edoc2Flow-web/process/manageMent/getProcessGroup`);
};

// 添加分组
export const addGroup = (params: any) => {
    return request('/edoc2Flow-web/process/manageMent/saveOrUpdateProcessGroup', {
        method: 'POST',
        data: {
            appId: params.appId,
            groupId: params.groupId,
            groupName: params.groupName,
            isShow: params.isShow,
            mark: params.mark,
        },
    });
};

// 编辑表单后保存
export const saveProcessManage = (params: {
    formKey: string;
    groupId: string;
    name: string;
    proLcon: string;
    mark?: string;
}) => {
    return request('/edoc2Flow-web/process/manageMent/saveOrupdateProcess', {
        method: 'POST',
        data: params,
    });
};

// 绑定流程页面
export const bindWorkFlowPage = (params: {
    siteOutId: string,
    dataVersionId: string,
    outId: string,
    wfKey: string,
}) => {
    return request(`/inbiz/api/services/wfpage/v${version}/wfPage/bindWfPage`, {
        method: 'POST',
        data: {
            ...params,
            isDevelopLatest: true
        },
    });
};